/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GPSTracker;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import sUtils.*;

/**
 *
 * @author thomas.s
 */
public class ScreenSaver extends Canvas {

    GPSTrackerView parent;
    GPSTrackerThread gpx;
    GPSTrackerMenu menu;
    long lastPress = System.currentTimeMillis();
    int keyPress=0;
    int showInfo=10;
    Timer timer=new Timer();


    ScreenSaver ( GPSTrackerView p, GPSTrackerThread g, GPSTrackerMenu m )
    {
        parent = p;
        gpx = g;
        menu = m;
        timer.schedule(new updateTask(this), 0, 500 );
        setFullScreenMode(true);
    }

    class updateTask extends TimerTask {
        private ScreenSaver canvas;
        int gpxUpdate=0;
        updateTask( ScreenSaver c ){  canvas = c;  }
        public void run()
        {
          //  gpxUpdate--;
          //  if (gpxUpdate<=0) { gpx.updateLocator(); gpxUpdate=4; }
            if (showInfo>0) canvas.repaint();
        }
    }


    protected void paint( Graphics g )
    {
        int w = getWidth(), h = getHeight();
        g.setColor(0);
        g.fillRect(0,0,w,h);

        showInfo--;
        if (showInfo>0)
        {
            Font f = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
            int fh = f.getHeight();
            Calendar c = Calendar.getInstance();
            StringBuffer s = new StringBuffer(40);
            int hh = c.get(Calendar.HOUR_OF_DAY);
            int mm = c.get(Calendar.MINUTE);
            int ss = c.get(Calendar.SECOND);
            s.append(hh/10); s.append(hh%10); s.append(":");
            s.append(mm/10); s.append(mm%10); s.append(":");
            s.append(ss/10); s.append(ss%10);
            String t;
            int col = 0xffffff;
            if (showInfo==4) col=0xc0c0c0; else
            if (showInfo==3) col=0x808080; else
            if (showInfo==2) col=0x404040; else
            if (showInfo==1) col=0x202020;
            g.setColor(col);
            t = gpx.trackrecord ? "GPSTracker on" : "GPSTracker off";
            g.drawString(t,w/2-f.stringWidth(t)/2,h/2-fh-fh/2,0);
            
            t = Strings.toStr( (gpx.time/60000), 3, 0, ' ', "", ":") + Strings.toStr( (gpx.time/1000)%60, 2, 0, '0', "", "");
            t = t + Strings.toStr( gpx.dist*menu.dist_factor, 3, 1, ' '," ",menu.dist_name);
            g.drawString(t,w/2-f.stringWidth(t)/2,h/2-fh/2,0);

            t = s.toString();
            g.drawString(t, w/2-f.stringWidth(t)/2, h/2+fh/2, 0);
            
        }

    }

    public void pointerPressed( int x, int y )
    {
        //parent.vibrate();
        showInfo=30;
    }

    public void pointerDragged( int x, int y )
    {
    }

    public void pointerReleased( int x, int y )
    {
    }

    public void keyPressed( int key )
    {
        if ( (System.currentTimeMillis()-lastPress)<500 )
        {
            //parent.vibrate();
            keyPress++;
            if (keyPress==2) parent.endScreenSaver();
        }
        else
            keyPress=0;
        lastPress = System.currentTimeMillis();
    }
}
